/**

 @Name：CLTPHP 模型
 @Author：chichu
 @Site：http://www.cltphp.com/admin/
 @License：LPPL
 */

layui.define(['admin', 'table', 'util','form'], function (exports) {
    var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element,form = layui.form,mid = $('#mid').val();
    //全部数据
    tableIn = table.render({
        elem: '#list',
        url: '/shengteng/field/index',
        where:{mid:mid},
        method: 'post',
        cols: [[
            {field: 'field', title: '字段名', width: 120,fixed: true},
            {field: 'name', title: '别名', width: 100},
            {field: 'type', title: '字段类型', width: 100},
            {field: 'issystem', align: 'center', title: '系统字段', width: 100, toolbar: '#isSystem'},
            {field: 'required', align: 'center', title: '必填', width: 80, toolbar: '#required'},
            {field: 'sort', align: 'center', title: '排序', width: 80, edit:'text'},
            {width: 190, align: 'center', toolbar: '#action'}
        ]]
    });
    //修改字段
    table.on('edit(list)', function(obj){
        var param={};
        param[obj.field] = obj.value;
        param['id'] = obj.data.id;
        cf(param);
    });

    function cf(param){
        admin.req({
            url: '/shengteng/field/cf',
            data: param,
            type:'post',
            success: function (res) {
                if(res.code==0){
                    layer.msg(res.msg,{time:1000,icon:1});
                }
                tableIn.reload();
            }
        });
    }

    var active = {
        //调用添加页面
        add: function(){
            var index = layer.open({
                type: 2,
                content: '/shengteng/field/add.html?mid='+mid,
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index);
        }
    };
    $('.clt-auth-btns .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //添加提交
    form.on('submit(add-btn)', function(data){
        admin.req({
            url: '/shengteng/field/add/'
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });


    //监听工具条
    table.on('tool(list)', function(obj){
        var data = obj.data;
        var id = data.id;
        if(obj.event === 'stateyes'){
            admin.req({
                url: '/shengteng/field/status/'
                , type: 'post'
                , data: {id:id}
                , done: function (res) {
                    layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                        tableIn.reload();
                    });
                }
            });
        } else if(obj.event === 'edit'){
            var index = layer.open({
                type: 2,
                content: '/shengteng/field/edit/id/'+id+'/mid/'+mid,
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index);
        }else if(obj.event === 'del'){
            layer.confirm('您确定要删除该字段吗？', function(index){
                admin.req({
                    url: '/shengteng/field/del/'
                    , type: 'post'
                    , data: {'id':id}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if(res.code==0){
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        }
                    }
                });
            });
        }
    });
    //编辑提交
    form.on('submit(edit-btn)', function(data){
        admin.req({
            url: '/shengteng/field/edit/' //实际使用请改成服务端真实接口
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });

    exports('field', {});
});
